package main

import (
	"fmt"
	"gopdf/app/card"
	"gopdf/app/dd"
	"gopdf/app/file"
	"log"
	"strings"
	"sync"
)

func main2() {
	list := card.ReadExcel()

	// var wg sync.WaitGroup
	// card.WriteFile(list, &wg)
	// wg.Wait()

	results, err := card.SendRequestForFilesInDir()
	if err != nil {
		fmt.Println(err)
	}
	resultMap := make(map[string]card.Record) // 使用map key保护fileId

	for _, item := range results {
		fileId := strings.Split(item.FileName, ".")[0]

		for _, v := range list {
			// 连同 AdminPhotoFront 一起进行处理
			allFileIds := append(v.LegalRepPhotoFront, v.AdminPhotoFront...)

			for _, cfileId := range allFileIds {
				if cfileId == fileId {
					if item.Result["name"] == v.MemberName {
						record := card.Record{
							CustomerID:         v.CustomerID,
							MemberStatus:       v.MemberStatus,
							MemberID:           v.MemberID,
							MemberName:         v.MemberName,
							Province:           v.Province,
							City:               v.City,
							Area:               v.Area,
							Address:            v.Address,
							AdminPhotoFront:    v.AdminPhotoFront,
							LegalRepPhotoFront: v.LegalRepPhotoFront,
							FileId:             cfileId,
						}

						if card.ContainsString(item.Result, []string{"name", "sex", "nation", "birthday", "address"}, "签发机关") ||
							card.ContainsString(item.Result, []string{"name", "sex", "nation", "birthday", "address"}, "中华人民共和国") ||
							card.ContainsString(item.Result, []string{"name", "sex", "nation", "birthday", "address"}, "公安局") ||
							item.Result["id_number"] == nil || len(item.Result["id_number"].(string)) <= 0 {
							record.Up = false
						} else {
							record.Up = true
						}

						if record.Up && item.Result["address"] != nil {
							record.Address = item.Result["address"].(string)
						}

						if _, exists := resultMap[record.FileId]; !exists {
							resultMap[record.FileId] = record
						}
					}
				}
			}
		}
	}

	rows, colIndices, err := card.ReadAddressExcelFile()
	if err != nil {
		log.Fatalf("%v", err)
	}

	for key, record := range resultMap {
		if record.Up && len(record.Address) > 0 {
			addressInfo := card.ReArea(record.Address, rows, colIndices)
			record.Province = addressInfo.Province
			record.ProvinceCode = addressInfo.ProvinceCode
			record.City = addressInfo.City
			record.CityCode = addressInfo.CityCode
			record.Area = addressInfo.Area
			record.AreaCode = addressInfo.AreaCode
			record.Remaining = addressInfo.Remaining
			resultMap[key] = record
		}
	}

	var results_new []card.Record
	for _, record := range resultMap {
		results_new = append(results_new, record)
	}

	card.WriteExcel(results_new)
}

func main3() {
	list := file.ReadExcel()
	// var wg sync.WaitGroup
	// file.WriteFile(list, &wg)
	// wg.Wait()

	var parseWg sync.WaitGroup
	// 解析文件
	parseList := file.ParsePDF(&list, &parseWg)
	fmt.Println(parseList)

	file.UpdateExcel(parseList)
}

func main() {
	dd.Crawler()
}
